//
//  BusinessCategorySV.m
//  菜单切换
//
//  Created by LangLang on 17/11/24.
//  Copyright © 2017年 freeOrganization. All rights reserved.
//

#import "BusinessCategorySV.h"
#import "ItemButton.h"

const NSInteger KCategoryTag = 100;
static const NSInteger KPageCount = 8;//一页的个数
static const NSInteger KPageSection = 2;//一页的行数

@implementation BusinessCategorySV

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //1.设置背景颜色
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

+ (id)businessCategorySV
{
    return [[self alloc] init];
}

+ (BusinessCategorySV *)businessCategorySVWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray titleArray:(NSArray *)titleArray target:(id)target action:(SEL)action
{
    BusinessCategorySV *businessCategorySV = [self businessCategorySV];
    businessCategorySV.frame = frame;
    
    //1.添加UIScrollView
    UIScrollView *categoryScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    categoryScrollView.pagingEnabled = YES;
    categoryScrollView.tag = 1000;
    categoryScrollView.showsHorizontalScrollIndicator = NO;
    categoryScrollView.showsVerticalScrollIndicator = NO;
    NSInteger page = (titleArray.count + KPageCount - 1)/KPageCount;
    categoryScrollView.contentSize = CGSizeMake(frame.size.width * page, frame.size.height);
    [businessCategorySV addSubview:categoryScrollView];
    
   UIPageControl *  _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, frame.size.height - 20, WIDTH, 20)];
//    _pageControl.center = center;
    //背景颜色
    _pageControl .tag = 100;
    _pageControl.backgroundColor = [UIColor clearColor];
    //点点的颜色  (不是当前页的点的颜色)
    _pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    //点点的颜色  (当前页的点的颜色)
    _pageControl.currentPage = page;

    _pageControl.currentPageIndicatorTintColor = [UIColor lightGrayColor];
    //设置页数（点点的个数）
    
    if (titleArray.count<=8) {
         _pageControl.numberOfPages = 1;
    }else if (8<titleArray.count&&titleArray.count<=16){
        _pageControl.numberOfPages = 2;
    }else if (16<titleArray.count&&titleArray.count<=24){
        _pageControl.numberOfPages = 3;
    }else if (24<titleArray.count&&titleArray.count<=32){
        _pageControl.numberOfPages = 4;
    }
   
//    //设置当前页
//    _pageControl.currentPage = 0;
    _pageControl.transform = CGAffineTransformMakeRotation(M_PI);
    //点击事件
//    [_pageControl addTarget:self action:@selector(pageControlClick:) forControlEvents:UIControlEventValueChanged];
    
    //添加到父视图
    [businessCategorySV addSubview:_pageControl];
    
    //2.添加button
    CGFloat itemBtnW = frame.size.width/(KPageCount/KPageSection);
    CGFloat itemBtnH = frame.size.height/KPageSection+0.9;
    for (int i = 0; i < titleArray.count; i ++) {
        ItemButton *itemBtn = [ItemButton buttonWithType:UIButtonTypeCustom];
        itemBtn.tag = i + KCategoryTag;
        [itemBtn sd_setImageWithURL:ImageUrl(imageArray[i]) forState:UIControlStateNormal];
//        [itemBtn setImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
        [itemBtn setTitle:titleArray[i] forState:UIControlStateNormal];
        [itemBtn setTitleColor:[UIColor colorWithRed:99/255.0 green:99/255.0 blue:99/255.0 alpha:1.0] forState:UIControlStateNormal];
//        itemBtn.imageView.contentMode =  UIViewContentModeCenter;
        CGFloat itemBtnX = i/KPageCount *frame.size.width + (i%(KPageCount/KPageSection))*itemBtnW;
        CGFloat itemBtnY = ((i - (i/KPageCount)*KPageCount)/(KPageCount/KPageSection))*itemBtnH;
        itemBtn.frame = CGRectMake(itemBtnX, itemBtnY, itemBtnW, itemBtnH);
        [itemBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        [categoryScrollView addSubview:itemBtn];
    }
    
    return businessCategorySV;
}


@end
